package com.example.xianji.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.example.xianji.activity.Main_FuWu_DetailsActivity;
import com.example.xianji.R;
import com.example.xianji.bean.FuWu;
import com.example.xianji.utils.ImageLoadUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FuWu_Adapter extends BaseAdapter {
private Context mContext;
private ArrayList<FuWu> list;
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
private   DisplayImageOptions options;


	public FuWu_Adapter(Context mContext, ArrayList<FuWu> list) {
	super();
	this.mContext = mContext;
	this.list = list;
	options = new DisplayImageOptions.Builder()
	/*.showImageOnLoading(R.drawable.touxiang)
	.showImageForEmptyUri(R.drawable.touxiang)
	.showImageOnFail(R.drawable.touxiang)*/
	.cacheInMemory(true)
	.cacheOnDisk(true)
	.considerExifParams(true)
	.bitmapConfig(Config.RGB_565)
	.delayBeforeLoading(100)
	.displayer(new RoundedBitmapDisplayer(10)).build();
}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder mHolder;
		if(convertView==null){
			mHolder=new Holder();
			convertView=View.inflate(mContext,R.layout.fuwu_listview_layout, null);
			mHolder.fuwu_title=(TextView) convertView.findViewById(R.id.fuwu_title);
			mHolder.fuwu_gongsi_name=(TextView) convertView.findViewById(R.id.fuwu_gongsi_name);
			mHolder.fuwu_tag=(TextView) convertView.findViewById(R.id.fuwu_tag);
			mHolder.fuwu_place=(TextView) convertView.findViewById(R.id.fuwu_place);
			mHolder.fuwu_image=(ImageView)convertView.findViewById(R.id.fuwu_image);
			convertView.setTag(mHolder);
		}
		mHolder=(Holder) convertView.getTag();
		mHolder.fuwu_title.setText(list.get(position).getTitle());
		mHolder.fuwu_gongsi_name.setText(list.get(position).getBtitle());
		mHolder.fuwu_tag.setText(list.get(position).getCat_name());
		mHolder.fuwu_place.setText(list.get(position).getProvince_name()+list.get(position).getCity_name());
		ImageViewAware imageviewaware=new ImageViewAware(mHolder.fuwu_image, false);
		//清楚Imageview 设置的图片和从Bitmap 内存缓存中获取
		ImageLoadUtil.loadImageByPath(options, list.get(position).getImgurl(), imageviewaware, animateFirstListener);
		
		final int b=position;
		
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(mContext,Main_FuWu_DetailsActivity.class);
				intent.putExtra("fuwu_details_id",list.get(b).getId());
				mContext.startActivity(intent);
			}
		});
		return convertView;
	}
	
class Holder{
	TextView fuwu_title;
	TextView fuwu_gongsi_name;
	TextView fuwu_tag;
	TextView fuwu_place;
	ImageView fuwu_image;
}

public static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
	public static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

	@Override
	public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
		if (loadedImage != null) {
			ImageView imageView = (ImageView) view;
			boolean firstDisplay = !displayedImages.contains(imageUri);
			if (firstDisplay) {
				FadeInBitmapDisplayer.animate(imageView, 200);
				displayedImages.add(imageUri);
			}
		}
	}
	
}

}
